//
//  CSSStatusTableViewCell.swift
//  weibo28
//
//  Created by chushushan on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

import SnapKit
/*
 原创微博视图
 转发微博视图
 底部toolBar

*/



class CSSStatusTableViewCell: UITableViewCell {

    //记录toolbar  TOP 的约束
    var toolbarTop: Constraint?
    
    
    //设置数据
    var statusViewModel: CSSStatusViewModel? {
    
        didSet {
        
            //原创微博的数据
            originalView.statusViewModel = statusViewModel
            
            //卸载约束
            toolbarTop?.uninstall()
            
            //判断转发微博是否有数据
            if statusViewModel?.status?.retweeted_status != nil {
                //转发微博的数据
                retweetView.statusViewModel = statusViewModel
            
                retweetView.hidden = false
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                 self.toolbarTop =  make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
            
            //隐藏转发微博视图
                retweetView.hidden = true
                
                //更新toolBar的约束
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.toolbarTop =  make.top.equalTo(originalView.snp_bottom).constraint
                })
            
            }
            

            
            //toobar
            toolBar.statusViewModel = statusViewModel
            
//            print(statusViewModel?.status?.retweeted_status?.pic_urls)
        }
        
    }
    
    
   //原创微博视图
    private lazy var originalView: CSSStatusOriginalView = CSSStatusOriginalView()
    //微博底部的toolbar
    private lazy var toolBar: CSSStatusToolBar = CSSStatusToolBar()
    //转发微博视图
    private lazy var retweetView: CSSStatusRetweetView = CSSStatusRetweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子控件设置约束
    private func setupUI() {
    
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加原创微博
        contentView.addSubview(originalView)
    
        //添加转发微博视图
        contentView.addSubview(retweetView)
        
        //添加底部toolbar
        contentView.addSubview(toolBar)
      
        
        //原创微博的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.left.right.equalTo(contentView)
            
        }
        
        //转发微博视图约束
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        
        
        //底部toolbar的约束
        toolBar.snp_makeConstraints { (make) -> Void in
            
           self.toolbarTop = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(40)
        }
        
        //关键约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.left.right.equalTo(self)
        }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

}
